﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace Presentation
{   

    public partial class TraCuuPhong : Form
    {
        private List<PhongDTO> dsPhong = PhongBUS.layDSPhong();
        private List<PhongDTO> dsTimDuoc = new List<PhongDTO>();
        public TraCuuPhong()
        {
            InitializeComponent();
        }

        private void TraCuuPhong_Load(object sender, EventArgs e)
        {
            LayDSLoaiPhong();
            
        }

        struct MangTimKiem
        {
            public int flag;
            public string text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            MangTimKiem[] mang = new MangTimKiem[6];                //phan tu 0: MaPhong
            for (int i = 0; i < 6; i++)                             //phan tu 1: TenPhong
            {                                                       //phan tu 2: TenLoaiPhong
                mang[i].flag = 0;                                   //phan tu 3: SoKhachHienCo
            }                                                       //phan tu 4: DonGiaDau
                                                                    //phan tu 5: DonGiaCuoi
            if (tb_MaPhong.Text.Trim() != "")
            {
                mang[0].text = tb_MaPhong.Text.Trim();
                mang[0].flag = 1;
            }
            else
                mang[0].text = null;

            if (tb_TenPhong.Text.Trim() != "")
            {
                mang[1].text = tb_TenPhong.Text.Trim();
                mang[1].flag = 1;
            }
            else
                mang[1].text = null;

            if (cbb_TenLoaiPhong.SelectedIndex >= 1)
            {
                LoaiPhongDTO loaiPhongDuocChon = (LoaiPhongDTO)cbb_TenLoaiPhong.Items[cbb_TenLoaiPhong.SelectedIndex];
                mang[2].text = loaiPhongDuocChon.TenLoaiPhong;
                mang[2].flag = 1;
            }
            else
                mang[2].text = null;

            if (tb_SoKhachHienCo.Text.Trim() != "")
            {
                mang[3].text = tb_SoKhachHienCo.Text.Trim();
                mang[3].flag = 1;
            }
            else
                mang[3].text = null;

            if (tb_DonGiaDau.Text.Trim() != "")
            {
                mang[4].text = tb_DonGiaDau.Text.Trim();
                mang[4].flag = 1;
            }
            else
                mang[4].text = null;

            if (tb_DonGiaCuoi.Text.Trim() != "")
            {
                mang[5].text = tb_DonGiaCuoi.Text.Trim();
                mang[5].flag = 1;
            }
            else
                mang[5].text = null;


            
            if ( dsPhong.Count() != 0)                   
            {
                for (int i = 0; i < dsPhong.Count(); i++)
                {
                    int kq=0;
                    for (int j = 0; j < 6; j++)
                    {
                        if (mang[j].flag == 1)
                        {
                            if (j == 0)
                                if (dsPhong[i].MaPhong != int.Parse(mang[j].text))
                                    kq=1;
                            if (j == 1)
                                if (dsPhong[i].TenPhong != mang[j].text)
                                    kq = 1;
                            if (j == 2)
                                if (dsPhong[i].LoaiPhong.TenLoaiPhong != mang[j].text)
                                    kq = 1;
                            if (j == 3)
                                if (dsPhong[i].SoKhachHienCo != int.Parse(mang[j].text))
                                    kq = 1;
                            if (j == 4)
                                if (dsPhong[i].LoaiPhong.DonGia < int.Parse(mang[j].text))
                                    kq = 1;
                            if (j == 5)
                                if (dsPhong[i].LoaiPhong.DonGia > int.Parse(mang[j].text))
                                    kq = 1;
                        }
                    }
                    if (kq == 0)
                        dsTimDuoc.Add(dsPhong[i]);
                }
            }
            else
            {
                MessageBox.Show("Không tồn tại dữ liệu!!!", "Thông Báo");
            }
            bool kiemtra =layDSPhongTimDuoc();
            if (kiemtra == false)
                MessageBox.Show("Không có phòng cần tìm");
            
            dsTimDuoc = new List<PhongDTO>();
        }

        private bool layDSPhongTimDuoc()
        {
            Grid_DSPhong.Rows.Clear();
            if (dsTimDuoc.Count() != 0)
            {
                for (int i = 0; i < dsTimDuoc.Count(); i++)
                {
                    Grid_DSPhong.Rows.Add(i+1,dsTimDuoc[i].MaPhong, dsTimDuoc[i].TenPhong, dsTimDuoc[i].LoaiPhong.TenLoaiPhong, dsTimDuoc[i].LoaiPhong.DonGia,dsTimDuoc[i].GhiChu, dsTimDuoc[i].SoKhachHienCo);
                    //Grid_DSPhong.Rows[GridDSPhong.RowCount - 1].Tag = dsPhong[i];
                }
                return true;
            }
            else
                return false;
            

        }


        private void LayDSLoaiPhong()
        {
            List<LoaiPhongDTO> dsLoaiPhong = LoaiPhongBUS.layDSLoaiPhong();
            LoaiPhongDTO none = new LoaiPhongDTO();
            dsLoaiPhong.Insert(0, none);
            cbb_TenLoaiPhong.DataSource = dsLoaiPhong;
            cbb_TenLoaiPhong.DisplayMember = "TenLoaiPhong";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, Application.StartupPath + "\\Help.chm");
        }

    }
}
